﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using TDT.Land.Utilities;
using TDT.Land.BusinessLogic;
using TDT.Land.Models;
using TDT.Land.Entity;
using System.IO;
using System.Data.Objects.SqlClient;

namespace TDT.Land.Web.Controllers
{
    public class BuildingController : BaseController
    {
        //
        // GET: /Building/
        // hien thi list buiding
        [CustomAuthorize(Roles = "3")]
        public ActionResult Index()
        {
           //var isAllowed= SecurityRepo.Instance.CheckPermission(Guid.Parse("F53DB0BA-B6AF-42BA-9322-2E9253C2262F"), Helper.Security.PrivilegeType.Create, "Cat_District");
            
            string buidingType = CategoryType.W_BUIDING_TYPE.ToString();
            var lst_buiding = AdminRepo.Instance.GetProBuildingByType(buidingType).OrderByDescending(m => m.DateCreate).ToList();


            //var aaa = AdminRepo.Instance.GetProBuildings().OrderByDescending(m => m.DateCreate).Take(20).ToList();
            //foreach (var item in aaa)
            //{
            //    if (!string.IsNullOrEmpty(item.BuildingType))
            //    {
            //        item.BuildingType = Commons.EnumTranslate<CategoryType>(item.BuildingType);
            //    }
            //}

            ViewBag.building = lst_buiding;

            return View();        
        }

        [CustomAuthorize(Roles = "3")]
        public ActionResult GetmoreBuilding(int NumPage, int NumRow)
        {
            var aaa = AdminRepo.Instance.GetProBuildings().OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow);
            //foreach (var item in aaa)
            //{
            //    if (!string.IsNullOrEmpty(item.BuildingType))
            //    {
            //        item.BuildingType = Commons.EnumTranslate<CategoryType>(item.BuildingType);
            //    }
            //}

            ViewBag.building = aaa;
            
            return PartialView("UC_EditBuildingList");        
        }

        [CustomAuthorize(Roles = "3")]
        public ActionResult openEdit(string id)
        {
            var lstSelectListItem = Commons.GetSelectList<CategoryType>();
            // List<Cat_LandModel> lstType = AdminRepo.Instance.GetCatLands(CategoryType.W_BUIDING_TYPE.ToString());
            //ViewBag.select_Type = Commons.ConvertToSelectListItems(lstType, Cat_LandModel.FieldName.Id, Cat_LandModel.FieldName.LandCategoryName);
            ViewBag.select_Type = lstSelectListItem;


            ViewBag.docBody = null;
            Pro_Building buildingInfo = new Pro_Building();
            if (!string.IsNullOrEmpty(id) && id != Guid.Empty.ToString())
            {
                //Guid buildingId = new Guid("2770F5FF-7E35-41E6-8826-25379D89F04D");
                Guid buildingId = new Guid(id);
                var ext = AdminRepo.Instance.GetProBuildingById(buildingId);
                if (ext != null)
                {
                    buildingInfo.Description = ext.Description;
                    buildingInfo.BuildingName = ext.BuildingName;
                    buildingInfo.ShortDescription = ext.ShortDescription;
                    buildingInfo.BuildingType = ext.BuildingType;
                    buildingInfo.ID = ext.ID;
                    buildingInfo.Code = ext.Code;

                    ViewBag.select_Type = Commons.GetSelectList<CategoryType>(ext.BuildingType);
                }

            }
            return View("UC_EditBuilding", buildingInfo);
        }

        [CustomAuthorize(Roles = "3")]
        public ActionResult openCreate()
        {
            ViewBag.docBody = null;
            return View("UC_EditBuilding");
        }

        [CustomAuthorize(Roles = "2")]
        public ActionResult DeleteBuiding(string id)
        {

            string saveTT = "00";
            bool statusSave = false;
            if (!string.IsNullOrEmpty(id))
            {
                Guid BDID = new Guid(id);
                statusSave = AdminRepo.Instance.RemoveProBuilding(BDID, UserName);
            }

            if (statusSave)
                saveTT = "01";

            return Content(saveTT);
        }


        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SubmitBuilding(Pro_Building test, Stream testupload, FormCollection forms)
        {
            var isSuccess = false;

            Guid buildingId = new Guid("2770F5FF-7E35-41E6-8826-25379D89F04D");
            var ext = AdminRepo.Instance.GetProBuildingById(buildingId);
            Pro_Building buildingInfo = new Pro_Building
            {
                Description = test.Description,
                BuildingName = test.BuildingName,
                ShortDescription = test.ShortDescription,               
                ID = test.ID
                //ID = ext.Id
            };
            var result = new Pro_Building();
            if (buildingInfo != null && buildingInfo.ID == Guid.Empty)
            {
                //add
                buildingInfo.DateCreate = DateTime.Now;
                buildingInfo.UserCreate = UserName;
                result = AdminRepo.Instance.AddProBuilding(buildingInfo, UserName);
            }
            else
            {
                //update
                buildingInfo.DateUpdate = DateTime.Now;
                buildingInfo.UserUpdate = UserName;
                isSuccess = AdminRepo.Instance.UpdateProBuilding(buildingInfo, UserName);
            }
            return null;
        }
        

        // khong dung hàm dosave
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult doSave(Pro_Building model, HttpPostedFileBase NAMEbtnUpload, FormCollection forms)
        {
            var isSuccess = false;
            var imgurl = Upload();
            if(!string.IsNullOrEmpty(imgurl))
                model.ImageFile = imgurl;
            var status = "00";
            if (model.ID != Guid.Empty)
            {
                //var ext = AdminRepo.Instance.GetProBuildingById(model.ID);
                //if (ext != null)
                //{
                //    //ID = ext.Id
                //}

                //update
                model.DateUpdate = DateTime.Now;
                isSuccess = AdminRepo.Instance.UpdateProBuilding(model, UserName);
                if (isSuccess == true)
                {
                    status = "01";
                }
            }
            else
            {
                model.ID = Guid.NewGuid();
                model.DateCreate = DateTime.Now;
                //add
                var result = AdminRepo.Instance.AddProBuilding(model, UserName);
                if (result != null)
                {
                    status = "01";
                }
            }
            return Content(status);
        }
        
        public string Upload()
        {
            var status = "";
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
                    file.SaveAs(path);
                    status = fileName;
                }
            }
            return status;
        }
        
        #region user site

        public ActionResult viewProject(string url)
        {
            var ext = AdminRepo.Instance.GetProBuildingByCode(url, UserName);
            ViewBag.Bcontent = ext;           
            return View("Us_viewDetail");
        }

        public ActionResult viewNewDetail(string url)
        {
            var ext = AdminRepo.Instance.GetProBuildingByCode(url, UserName);
            ViewBag.Bcontent = ext;

            string typeNews = CategoryType.W_NEW_TYPE.ToString();
            var moreItem = AdminRepo.Instance.GetProBuildings().Where(v => v.BuildingType == typeNews).Take(5).ToList();
            ViewBag.ItemMore = moreItem;
            return View("US_DetailNews");
        }

        #endregion


        #region admin Get List Tin tuc

        [CustomAuthorize(Roles = "2")]
        public ActionResult ManagerNews()
        {
            string buidingType = CategoryType.W_NEW_TYPE.ToString();
            var pro_buiding = AdminRepo.Instance.GetProBuildingByType(buidingType).OrderByDescending(m => m.DateCreate).Take(20).ToList();

            ViewBag.building = pro_buiding;
            return View("AD_View_News");
        }

        [CustomAuthorize(Roles = "2")]
        public ActionResult openEditNews(string id)
        {
            var lstSelectListItem = Commons.GetSelectList<CategoryType>();
            // List<Cat_LandModel> lstType = AdminRepo.Instance.GetCatLands(CategoryType.W_BUIDING_TYPE.ToString());
            //ViewBag.select_Type = Commons.ConvertToSelectListItems(lstType, Cat_LandModel.FieldName.Id, Cat_LandModel.FieldName.LandCategoryName);
            ViewBag.select_Type = lstSelectListItem;


            ViewBag.docBody = null;
            Pro_Building buildingInfo = new Pro_Building();
            if (!string.IsNullOrEmpty(id) && id != Guid.Empty.ToString())
            {
                //Guid buildingId = new Guid("2770F5FF-7E35-41E6-8826-25379D89F04D");
                Guid buildingId = new Guid(id);
                var ext = AdminRepo.Instance.GetProBuildingById(buildingId);
                if (ext != null)
                {
                    buildingInfo.Description = ext.Description;
                    buildingInfo.BuildingName = ext.BuildingName;
                    buildingInfo.ShortDescription = ext.ShortDescription;
                    buildingInfo.BuildingType = ext.BuildingType;
                    buildingInfo.ImageFile = ext.ImageFile;
                    buildingInfo.ID = ext.ID;
                    buildingInfo.Code = ext.Code;
                    buildingInfo.IsSlideShow = ext.IsSlideShow;

                    ViewBag.select_Type = Commons.GetSelectList<CategoryType>(ext.BuildingType);
                }

            }
            return View("UC_Edit_News", buildingInfo);
        }
        
        public List<Pro_Building> GetNewsForHome() {

            string buidingType = CategoryType.W_NEW_TYPE.ToString();
            List<Pro_Building> lstBuidingTop = AdminRepo.Instance.GetProBuildingByType(buidingType).OrderByDescending(m => m.DateCreate).Take(5).ToList();

            return lstBuidingTop;
        }


        #endregion


    }
}
